/*
 * @Author: lei
 * @Date: 2021-08-02 10:22:52
 * @LastEditTime: 2021-08-02 10:55:55
 * @LastEditors: Please set LastEditors
 * @Description: sizeof、strlen使用
 * @FilePath: /code/sizeof_strlen.cpp
 */
#include <iostream>
#include <string>
/**
 * @description: 测试sizeof函数
 * @param {std::string} a
 * @return {void}
 */
void test1(std::string a)
{
    std::cout<<"--------test1测试开始--------"<<std::endl;
    std::cout<<"sizeof(a)= "<<sizeof(a)<<std::endl;
    std::cout<<"sizeof(int)= "<<sizeof(int)<<std::endl;
    std::cout<<"sizeof(double)= "<<sizeof(double)<<std::endl;
    std::cout<<"--------test1测试结束--------"<<std::endl;
}
/**
 * @description: 测试strlen函数
 * @param {std::string} a
 * @return {void}
 */
void test2(std::string a)
{
    std::cout<<"--------test2测试开始--------"<<std::endl;
    //std::cout<<"strlen(a)= "<<strlen(a)<<std::endl;编译错误
    std::cout<<"strlen(a)= "<<strlen(a.c_str())<<std::endl;
    std::cout<<"--------test2测试结束--------"<<std::endl;

}
/**
 * @description: 数组作为参数传递给函数会退化为指针，strlen与sizeof对此的不同处理
 * @param {char} arr
 * @return {void}
 */
void test3(char arr[])
{
    std::cout<<"--------test3测试开始--------"<<std::endl;
    std::cout << "sizeof(arr)= " << sizeof(arr) << std::endl; // warning: 'sizeof' on array function parameter 'arr' will return size of 'char*' .
    std::cout << "strlen(arr)= " << strlen(arr) << std::endl; 
    std::cout<<"--------test3测试结束--------"<<std::endl;
}

int main()
{
    std::string s="privacy";
    test1(s);
    test2(s);
    char arr[20]="protection";
    test3(arr);
    return 0;
}
/*
--------test1测试开始--------
sizeof(a)= 24
sizeof(int)= 4
sizeof(double)= 8
--------test1测试结束--------
--------test2测试开始--------
strlen(a)= 7
--------test2测试结束--------
--------test3测试开始--------
sizeof(arr)= 8
strlen(arr)= 10
--------test3测试结束--------
*/